//
//  PromotionActivityView.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/5.
//

import UIKit
import FSPagerView


typealias BlockShareActivity = (_ model: ActivityListModel) -> Void
class PromotionActivityView: UIView {
    var pagerView: FSPagerView!
    var pageControl: TYPageControl!
    var hideBtn: UIButton!
    var dataArray: [ActivityListModel] = []
    var shareActivityDataBlock: BlockShareActivity!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let contor = UIControl(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        contor.backgroundColor = .black
        contor.alpha = 0.7
        contor.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.addSubview(contor)
        
        // Create a pager view
        self.pagerView = FSPagerView(frame: CGRect(x: 0, y: (kScreenHeight - 366)/2, width: kScreenWidth, height: 366))
        self.pagerView.dataSource = self
        self.pagerView.delegate = self
        self.pagerView.transformer = FSPagerViewTransformer(type:.linear)
        let transform = CGAffineTransform(scaleX: 0.6, y: 0.75)
        self.pagerView.itemSize = self.pagerView.frame.size.applying(transform)
        self.pagerView.decelerationDistance = FSPagerView.automaticDistance
        let width = (kScreenWidth - 60)
        self.pagerView.itemSize = CGSize(width: width, height: 366)
        self.pagerView.register(PromotionActivityCell.self, forCellWithReuseIdentifier: "cell")
        self.addSubview(self.pagerView)
        
        
        self.pageControl = TYPageControl(frame: CGRect(x: 0, y: self.pagerView.frame.maxY + 6, width: kScreenWidth, height: 4))
        self.pageControl.currentPageIndicatorSize = CGSize(width: 4, height: 4)
        self.pageControl.pageIndicatorSize = CGSize(width: 4, height: 4)
        self.pageControl.backgroundColor = .clear
        self.pageControl.currentPage = 0
        self.pageControl.currentPageIndicatorTintColor = .white
        self.pageControl.pageIndicatorTintColor = UIColor(hex: 0xFFFFFF,alpha: 0.1)//不活跃指示器颜色
//        self.pageControl.pageIndicatorImage = UIImage.init(named: "icon_banner_dot")
//        self.pageControl.currentPageIndicatorImage = UIImage.init(named: "icon_banner_dot_white")
        self.addSubview(self.pageControl)
        
        
        
        self.hideBtn = UIButton(frame: CGRect(x: (kScreenWidth - 30)/2, y:   self.pagerView.frame.maxY + 20, width: 30, height: 30))
        self.hideBtn.setImage(UIImage(named: "icon_hide"), for: .normal)
        self.hideBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.addSubview(self.hideBtn)
        
    }
    
    //setModel
    func setData(dataArray: [ActivityListModel]){
        self.dataArray = dataArray
        if self.dataArray.count > 1 {
            self.pageControl.numberOfPages = self.dataArray.count
        }else{
            self.pageControl.numberOfPages = 0
        }
        self.pagerView.reloadData()
        
        //block
        _ = self.dataArray.map({ model in
            model.shareActivityBlock = { [weak self] in
                self?.close()
                self?.shareActivityDataBlock?(model)
            }
        })
    }

    
    @objc func close(){
        UIView.animate(withDuration: 0.3, animations: {() -> Void in
            self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
            self.backgroundColor = UIColor.clear
            self.alpha = 0
        }, completion: {(_ finished: Bool) -> Void in
            self.removeFromSuperview()
        })
    }
    
   
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension PromotionActivityView: FSPagerViewDataSource,FSPagerViewDelegate{
    
     func numberOfItems(in pagerView: FSPagerView) -> Int {
         return self.dataArray.count
    }
    
     func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! PromotionActivityCell
        if self.dataArray.count > index{
            let model = self.dataArray[index]
            cell.setModel(model: model)
        }
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
    }
    
 
    
    func pagerViewDidEndDecelerating(_ pagerView: FSPagerView){
        print("滑动到了第\(pagerView.currentIndex)个row")
        self.pageControl.currentPage = pagerView.currentIndex
    }
    
}
